package com.pay.ioopos.activity;

import android.content.Intent;
import android.os.Bundle;

import com.pay.ioopos.DataFactory;
import com.pay.ioopos.DataStore;
import com.pay.ioopos.KeyInfo;
import com.pay.ioopos.PayMode;
import com.pay.ioopos.PwdListener;
import com.pay.ioopos.R;
import com.pay.ioopos.fragment.ComPwdValidFragment;
import com.pay.ioopos.fragment.ModeAmountFragment;
import com.pay.ioopos.fragment.ModeAmountFragment.OnAmountListner;
import com.pay.ioopos.fragment.TipVerticalFragment;

import static com.pay.ioopos.KeyInfo.KEY_CANCEL;
import static com.pay.ioopos.KeyInfo.KEY_ENTER;
import static com.pay.ioopos.PayMode.FIXED;
import static com.pay.ioopos.PayMode.NORMAL;
import static com.pay.ioopos.fragment.TipFragment.TipType.SUCCESS;

public class ModeActivity extends AbstractActivity implements OnAmountListner, PwdListener {
    private PayMode mode;
    private String amount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mode);
        Intent intent = getIntent();
        int i = intent.getIntExtra("mode", 0);
        mode = PayMode.values()[i];
        setMainFragment(new ModeAmountFragment(mode, this));

    }

    @Override
    public boolean onKeyDown(KeyInfo keyInfo) {
        if (mainFragment instanceof ModeAmountFragment) {

        } else if (mainFragment instanceof ComPwdValidFragment) {
            ComPwdValidFragment fragment = ((ComPwdValidFragment)mainFragment);
            fragment.onKeyDown(keyInfo);
            if (keyInfo == KEY_CANCEL) {
                onCancel();
            }
        } else if (mainFragment instanceof TipVerticalFragment) {
            if (keyInfo == KEY_ENTER || keyInfo == KEY_CANCEL) {
                onCancel();
            }
        } else {
            if (keyInfo == KEY_CANCEL) {
                onCancel();
            }
        }
        return false;
    }

    @Override
    public void onFinish(String amount) {
        this.amount = amount;
        ComPwdValidFragment fragment = new ComPwdValidFragment();
        fragment.setPwdListener(this);
        setMainFragment(fragment);
    }

    @Override
    public void onCancel() {
        Intent intent = new Intent(this, SettingActivity.class);
        startActivity(intent);
    }

    @Override
    public void onPwdValid(String pwd) {
        DataStore store = DataFactory.getDataStore();
        if (mode == NORMAL) {
            store.setMaxAmount(amount);

        } else if (mode == FIXED) {
            store.setFixAmount(amount);
        }
        store.setMode(mode);
        setMainFragment(new TipVerticalFragment(SUCCESS, "已切换至" + mode.getText()));
    }
}
